﻿using GalaSoft.MvvmLight.Command;
using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.EAPClient.Model;
using Glorysoft.EAP.EAPClient.View;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Glorysoft.EAP.EAPClient.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class TrackOutLossViewModel : PopupWindowVM
    {
        private readonly Logger ServiceLogger = XfwContext.EapLogger;
        public delegate void CloseDelegate();
        public event CloseDelegate CloseEvent;
        public static List<Core.Entities.UNITLIST> UnitList;
        public static List<Core.Entities.LOSSINFOLIST> loss;
        /// <summary>
        /// Initializes a new instance of the LoginViewModel class.
        /// </summary>
        string EQPPORT = string.Empty;
        public TrackOutLossViewModel()
        {

            if (ClientInfo.EQPInfo != null && ClientInfo.EQPInfo.LotList.Count > 0)
            {
                var lot = ClientInfo.EQPInfo.LotList[0];
                LotId = lot.lotId;
                productId = lot.productId;
                eqpid = lot.EqpId;
                loss = new List<Core.Entities.LOSSINFOLIST>();
                loss = lot.LOSSINFOLIST;
                UnitList = new List<Core.Entities.UNITLIST>();
                UnitList = lot.UNITLIST;
                defectedProduct = new ObservableCollection<DefectProduct>();
                if (LossSN.count != 0)
                {
                    foreach (var item in LossSN.LossInfoList)
                    {
                        int count = 0;
                        if (item.PIDLIST != null)
                        {
                            count = item.PIDLIST.Count;
                        }
                        defectedProduct.Add(new Model.DefectProduct()
                        {
                            code = item.LOSSCODE,
                            Desc = item.LOSSDESC,
                            qty = count.ToString(),
                        });
                    }
                }
                else
                {
                    foreach (var item in loss)
                    {
                        defectedProduct.Add(new Model.DefectProduct()
                        {
                            code = item.LOSSCODE,
                            Desc = item.LOSSDESC,
                            qty = "0",
                        });
                    }
                }
            }
        }
        public RunService ClientInfo
        {
            get
            {
                return RunService.Current;
            }
        }
        private string lotid;
        public string LotId
        {
            get { return lotid; }
            set
            {
                lotid = value;
                RaisePropertyChanged("LotId");
            }
        }
        private string productId;
        public string ProductId
        {
            get { return productId; }
            set
            {
                productId = value;
                RaisePropertyChanged("ProductId");
            }
        }
        private string eqpid;
        public string EqpId
        {
            get { return eqpid; }
            set
            {
                eqpid = value;
                RaisePropertyChanged("EqpId");
            }
        }
        private ObservableCollection<DefectProduct> defectedProduct = new ObservableCollection<DefectProduct>();
        public ObservableCollection<DefectProduct> DefectProduct
        {
            get { return defectedProduct; }
            set
            {
                defectedProduct = value;
                RaisePropertyChanged("DefectProduct");
            }
        }
        #region command

        private RelayCommand cancelCmd;
        public RelayCommand CancelCmd
        {
            get { return cancelCmd ?? (cancelCmd = new RelayCommand(Cancel)); }
        }
        public void Cancel()
        {
            try
            {
                if (CloseEvent != null)
                    CloseEvent();
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
            this.Close();
        }

        private RelayCommand confirmCmd;
        public RelayCommand ConfirmCmd
        {
            get { return confirmCmd ?? (confirmCmd = new RelayCommand(Confirm)); }
        }
        public void Confirm()
        {
            try
            {
                //List<DefectProduct> de = new List<DefectProduct>();
                //if (ClientInfo.EQPInfo != null && ClientInfo.EQPInfo.LotList.Count > 0)
                //{
                //    var lot = ClientInfo.EQPInfo.LotList[0];
                //    EAPClient eq = new EAPClient();
                //    eq.TrackOutByUser(lot.EqpId);
                //    lot.DefectProduct = new List<DefectProduct>();
                //    foreach (var defect in DefectProduct)
                //    {

                //        lot.DefectProduct.Add(defect);
                //    }
                //}
                //调取trackout接口
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
            //this.Close();
        }
        #endregion
    }
}